package easy;

import java.util.*;

public class Question1 {

    public static void main(String[] args) {
        Question1 q1 = new Question1();
        System.out.println(Arrays.toString(q1.twoSum(new int[]{3, 2, 4}, 6)));
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            map.put(i, nums[i]);
        }
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort(Comparator.comparingInt(Map.Entry::getValue));
        int min = list.get(0).getValue();
        int maxIndex = Math.min((int) list.stream().filter(e -> e.getValue() <= target - min).count(), list.size() - 1);
        for (int i = maxIndex; i >= 0; i--) {
            Map.Entry<Integer, Integer> ea = list.get(i);
            Optional<Map.Entry<Integer, Integer>> optional = list.stream().filter(x -> !x.equals(ea)).filter(e -> e.getValue() + ea.getValue() == target).findAny();
            if (optional.isPresent()) {
                int indexA = Math.min(ea.getKey(),optional.get().getKey());
                int indexB = Math.max(ea.getKey(),optional.get().getKey());
                return new int[]{indexA, indexB};
            }
        }
        return new int[]{-1, -1};
    }


}
